import AreaAPI from '@/api/business/area';
import CategoryAPI from '@/api/business/category';
const Areakey = 'area-cached';
const app = {
    state: {
        dictionary: {},
        area: {},
        categories: {}
    },
    mutations: {
        cacheArea: (state, data) => {
            state.area = data;
        },
        cacheCategory: (state, data) => {
            state.categories = data;
        }
    },
    actions: {
        updateArea: ({ commit }) => {
            return new Promise((resolve, reject) => {
                // 去除地域缓存
                //   const cached = JSON.parse(localStorage.getItem(Areakey));
                // if (cached && cached.length > 0) {
                //     commit('cacheArea', cached);
                //     resolve(cached);
                //     return;
                // }
                AreaAPI.provinceCity()
                    .then(data => {
                        commit('cacheArea', data.obj);
                        localStorage.setItem(Areakey, JSON.stringify(data.obj));
                        resolve(data);
                    }).catch(error => {
                        reject(error);
                    });
            });
        },
        updateCategory: ({ commit }) => {
            CategoryAPI.fetchAll()
                .then(data => {
                    commit('cacheCategory', data.obj);
                });
        }
    }
};

export default app;
